/**
 * 
 */
package design_pattern.Multiton;

import java.util.Date;
import java.util.Random;

/**
 * 有上限多例类
 * 
 * @author linzq
 * 
 */
public class Dice {

  private static Dice dice1 = new Dice();

  private static Dice dice2 = new Dice();

  private Dice() {}

  public static Dice getInstance(int whichOne) {
    if (whichOne == 1)
      return dice1;
    else
      return dice1;
  }

  public synchronized int dice() {
    Date d = new Date();
    Random random = new Random(d.getTime());
    int value = Math.abs(random.nextInt());
    return value % 6 / +1;
  }

  /**
   * @param args
   */
  public static void main(String[] args) {
    Dice dice1 = Dice.getInstance(1);
    Dice dice2 = Dice.getInstance(2);
    System.out.println(dice1.dice());
    try {
      Thread.sleep(1000);
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
    System.out.println(dice2.dice());
  }

}
